<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>TreeGrid with Footer - jQuery EasyUI Demo</title>
	<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="demo.css">
	<link rel="stylesheet" type="text/css" href="../themes/icon.css">
	
	<script type="text/javascript" src="../jquery-1.7.2.min.js"></script>
	<script type="text/javascript" src="../jquery.easyui.min.js"></script>
	<script type="text/javascript">
	
	var editingId;
        function edit(){
            if (editingId != undefined){
                $('#test').treegrid('select', editingId);
                return;
            }
            var row = $('#test').treegrid('getSelected');
            if (row){
                editingId = row.id
                $('#test').treegrid('beginEdit', editingId);
            }
        }
        function save(){
            if (editingId != undefined){
                var t = $('#test');
                t.treegrid('endEdit', editingId);
                editingId = undefined;
                var persons = 0;
                var rows = t.treegrid('getChildren');
                for(var i=0; i<rows.length; i++){
                    var p = parseInt(rows[i].persons);
                    if (!isNaN(p)){
                        persons += p;
                    }
                }
                var frow = t.treegrid('getFooterRows')[0];
                frow.persons = persons;
                t.treegrid('reloadFooter');
            }
        }
        function cancel(){
            if (editingId != undefined){
                $('#test').treegrid('cancelEdit', editingId);
                editingId = undefined;
            }
        }
	</script>
	
	<script>
		$(function(){
			$('#test').treegrid({
				title:'TreeGrid',
				iconCls:'icon-ok',
				width:700,
				height:250,
				rownumbers: true,
				animate:true,
				collapsible:true,
				fitColumns:true,
				url:'treegrid_data2.json',
				idField:'id',
				treeField:'name',
				showFooter:true,
				rowStyler:function(row){
					if (row.persons > 1){
						return 'background:#AAD684;color:#fff';
					}
				},
				columns:[[
	                {title:'Task Name',field:'name',width:180},
					{field:'persons',title:'Persons',width:60,align:'right'},
					{field:'begin',title:'Begin Date',width:80},
					{field:'end',title:'End Date',width:80,rowspan:2},
					{field:'progress',title:'Progress',width:120,rowspan:2,
					    formatter:function(value){
					    	if (value){
						    	var s = '<div style="width:100%;background:#fff;border:1px solid #ccc">' +
						    			'<div style="width:' + value + '%;background:red">' + value + '%' + '</div>'
						    			'</div>';
						    	return s;
					    	} else {
						    	return '';
					    	}
				    	}
					}
				]],
				
				toolbar:[{
					text:'append',
					iconCls:'icon-add',
					handler:function(){
						edit();
					}
				},'-',{
					text:'delete',
					iconCls:'icon-remove',
					handler:function(){
						var row = $('#test').treegrid('getSelected');
						if (row){
							var index = $('#test').treegrid('getRowIndex', row);
							$('#test').treegrid('deleteRow', index);
						}
					}
				},'-',{
					text:'accept',
					iconCls:'icon-save',
					handler:function(){
						$('#test').treegrid('acceptChanges');
					}
				},'-',{
					text:'reject',
					iconCls:'icon-undo',
					handler:function(){
						$('#test').treegrid('rejectChanges');
					}
				},'-',{
					text:'GetChanges',
					iconCls:'icon-search',
					handler:function(){
						var rows = $('#test').treegrid('getChanges');
						alert('changed rows: ' + rows.length + ' lines');
					}
				}]
				
			});
		});
	</script>
</head>
<body>
	<h2>TreeGrid</h2>
	<div class="demo-info" style="margin-bottom:10px">
		<div class="demo-tip icon-tip"></div>
		<div>Show summary information on treegrid footer.</div>
	</div>
	
	<table id="test"></table>
	
</body>
</html>